package com.sairobo.admin.modules.srblive.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.sairobo.base.common.utils.FormateUtils;
import com.sairobo.base.common.utils.ShiroUtils;
import com.sairobo.base.common.constant.MsgConstant;
import com.sairobo.base.common.entity.Page;
import com.sairobo.base.common.utils.CommonUtils;
import com.sairobo.base.common.utils.MultiUpload;
import com.sairobo.admin.modules.srblive.entity.SrbliveFileEntity;
import com.sairobo.admin.modules.srblive.manager.SrbliveFileManager;
import com.sairobo.admin.modules.srblive.pojo.param.PPTParam;
import com.sairobo.admin.modules.srblive.service.SrbliveFileService;
import com.sairobo.admin.modules.srblive.utils.LivePropertyUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.sairobo.base.common.entity.Query;
import com.sairobo.base.common.entity.R;

/**
 * 文件目录
 *
 * @author admin
 * @email sairobo
 * @url www.sairobo.com
 * @date 2018年1月09日 上午10:41:05
 */
@Service("srbliveFileService")
public class SrbliveFileServiceImpl implements SrbliveFileService
{

	@Autowired
	private SrbliveFileManager srbliveFileManager;

	@Override
	public Page<SrbliveFileEntity> listSrbliveFile(Map<String, Object> params)
	{
		Query query = new Query(params);
		Page<SrbliveFileEntity> page = new Page<>(query);
		srbliveFileManager.listSrbliveFile(page, query);
		return page;
	}

	@Override
	public R saveSrbliveFile(SrbliveFileEntity role)
	{
		int count = srbliveFileManager.saveSrbliveFile(role);
		return CommonUtils.msg(count);
	}

	@Override
	public R getSrbliveFileById(Long id)
	{
		SrbliveFileEntity srbliveFile = srbliveFileManager.getSrbliveFileById(id);
		return CommonUtils.msg(srbliveFile);
	}

	@Override
	public R updateSrbliveFile(SrbliveFileEntity srbliveFile)
	{
		int count = srbliveFileManager.updateSrbliveFile(srbliveFile);
		return CommonUtils.msg(count);
	}

	@Override
	public R batchRemove(Long[] id)
	{
		int count = srbliveFileManager.batchRemove(id);
		return CommonUtils.msg(id, count);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public R multiUpload(HttpServletRequest request,MultipartFile file, PPTParam param)
            throws IOException, InstantiationException, IllegalAccessException
	{
		String rootPath = LivePropertyUtil.instance.getMessage("ppt.upload.path");
		String openId = ShiroUtils.getUserEntity().getOpenId();
		String classroomId = param.getClassRoomId();

		List srbliveFileEntityes = null;
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("openId", openId);
		map.put("classroomId", classroomId);
		MultiUpload multiUpload =new MultiUpload() {
			 protected void wrapper(Object obj, Map<String, Object> map) {
				 SrbliveFileEntity entity=(SrbliveFileEntity)obj;
				 entity.setOpenId(openId);
				 entity.setClassroomId(classroomId);
				 MultipartFile file = (MultipartFile)map.get(FILE);
				 entity.setFileName(file.getOriginalFilename());
//				 entity.setFileSize(String.valueOf(file.getSize()));
				 entity.setFileSize(FormateUtils.getFormatSize(file.getSize()));
				 entity.setFileUrl((String)(map.get(PATH)));
			}
		};
		srbliveFileEntityes =  multiUpload.multiUpload(request, rootPath, SrbliveFileEntity.class, map);
		if(CollectionUtils.isEmpty(srbliveFileEntityes))
		{
			return R.error(MsgConstant.MSG_INIT_UPLOAD);
		}
		srbliveFileManager.insertBatch(srbliveFileEntityes);
		return CommonUtils.msg(srbliveFileEntityes);
	}
	

}
